import { _decorator, Component, Node, CCObject, Vec3, systemEvent, SystemEvent, EventMouse, v2,v3,AnimationComponent, SkeletalAnimationComponent, AudioSourceComponent } from "cc";
const { ccclass, property } = _decorator;

@ccclass("PlayerController")
export class PlayerController extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;
    @property({type: AnimationComponent})
    public bodyAnim: AnimationComponent = null;

    @property({type: SkeletalAnimationComponent})
    public cocosAnimation: SkeletalAnimationComponent = null;

    // @property({type: AudioSourceComponent})
    // public jumpAudio: AudioSourceComponent = null;

    private _startJump: boolean = false;
    private _jumpStep: number = 0;
    private _curJumpTime: number = 0;
    private _jumpTime: number = 0.3;
    private _curJumpSpeed: number = 0;
    private _curPos: Vec3 = v3();
    private _deltaPos: Vec3 = v3(0,0,0);
    private _targetPos: Vec3 = v3();
    public _isMoving: boolean = false;
    // 跳跃的步数
    public stepCount: number = 0;

    // 英雄停留时间计算
    public stopTime: number = 0;
    public gameOver: boolean = false;
    // 是否开始游戏
    public begin: boolean = false;
    start () {
        
    }
    public setInputActive(active: boolean) {
        if(active) {
            this.begin = true;
            systemEvent.on(SystemEvent.EventType.MOUSE_UP,this.onMouseUp,this);
        } else {
            systemEvent.off(SystemEvent.EventType.MOUSE_UP,this.onMouseUp,this);
        }
    }
    private onMouseUp(e: EventMouse): void {

        // this.cocosAnimation.play();
        if(this.begin) {
            console.log("开始播放音效");
            this.node.getComponent(AudioSourceComponent).play();
        }
        if(e.getButton() === 0) {
            // this.bodyAnim.play("oneStep");
            this.jumpByStep(1);
        } else if(e.getButton() === 2) {
            this.bodyAnim.play("twoStep");
            this.jumpByStep(2);
        }
    }
    public jumpByStep(step: number) {
        if(this._isMoving) {
            return;
        }
        this._startJump = true;
        this._jumpStep = step;
        this._curJumpTime = 0;
        this._curJumpSpeed = this._jumpStep / this._jumpTime;
        this.node.getPosition(this._curPos);
        Vec3.add(this._targetPos,this._curPos,v3(this._jumpStep,0,0));
        this.stepCount += step;
        this._isMoving = true;
        this.cocosAnimation.getState("cocos_anim_jump").speed = 5;
        this.cocosAnimation.play("cocos_anim_jump");

    }
    // 一次跳跃结束
    onOnceJumpEnd() {
        this._isMoving = false;
        this.node.emit("jumpEnd",this.stepCount);
        // 恢复默认动作
        this.cocosAnimation.play("cocos_anim_idle");
        // 重新开始计算停留时间
        this.stopTime = 0;
    }
    update (deltaTime: number) {
        if(!this._isMoving && this.begin) {
            this.stopTime += deltaTime;
            if(this.stopTime > 1) {
                this.gameOver = true;
                this.begin = false;
                this.stopTime = 0;
            }
        }
        if(this._startJump) {
            console.log("jump");
            this._curJumpTime += deltaTime;
            if(this._curJumpTime > this._jumpTime) {
                this.node.setPosition(this._targetPos);
                this._startJump = false;
                this.onOnceJumpEnd();
            } else {
                // this.node.getPosition(this._curPos);
                this._deltaPos.x = this._curJumpSpeed * deltaTime;
                // Vec3.add(this._curPos,this._curPos,this._deltaPos);
                Vec3.add(this._curPos,this._curPos,this._deltaPos);
                this.node.setPosition(this._curPos);
            }
        }
    }
}
